package lxs.swift.verify;


import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;

import java.util.function.Supplier;

public class Asserts {
    @Contract("null,_,_ -> fail")
    public static void notNull(@Nullable Object object, String message, Object... args) throws AssertionError {
        if (object == null) {
            FormattingTuple format = MessageFormatter.format(message, args);
            String formatMessage = format.getMessage();
            throw new AssertionError(formatMessage);
        }
    }
    public static void notEmpty(@Nullable Object object, String message, Object... args) throws AssertionError {
        if (Verifies.isEmpty(object)) {
            FormattingTuple format = MessageFormatter.format(message, args);
            String formatMessage = format.getMessage();
            throw new AssertionError(formatMessage);
        }
    }

    @Contract("false,_,_ -> fail")
    public static void expected(boolean state, String message, Object... args) throws AssertionError {
        if (!state) {
            FormattingTuple format = MessageFormatter.format(message, args);
            String formatMessage = format.getMessage();
            throw new AssertionError(formatMessage);
        }
    }

    @Contract("true,_,_ -> fail")
    public static void unexpected(boolean state, String message, Object... args) throws AssertionError {
        if (state) {
            FormattingTuple format = MessageFormatter.format(message, args);
            String formatMessage = format.getMessage();
            throw new AssertionError(formatMessage);
        }
    }

    @Contract("false,_ -> fail")
    public static <E extends Throwable> void expected(boolean state, Supplier<? extends E> supplier) throws E {
        if (!state) {
            throw supplier.get();
        }
    }

    @Contract("true,_ -> fail")
    public static <E extends Throwable> void unexpected(boolean state, Supplier<? extends E> supplier) throws E {
        if (state) {
            throw supplier.get();
        }
    }
}
